<?php
/*
 * Created on Mar 27, 2009
 * Author: ANH
 * fileName: Gallery.php
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
class gallery_Model_Gallery
{
	public function init()
	{
		//require (APP_PATH."/library/common.php");
	}
	
	/*
	 * Update $gallery['images'] metadata by adding $imageFile if needed
	 * If the file is the same, then just remain
	 * $imageFile should be stripped of the prefex APP_PATH;
	 * images => array( 0 => array ("filename" => $imageFile,
	 * 								"title" => ""))
	 */
	public function updateGalleryImageList($gallery, $imageFile)
	{
		if (!empty($gallery['images']))
			$images = unserialize ($gallery['images']);
		else
			$images = array();
		
		$filename = str_replace(APP_PATH . "/public/ustore", "/public/ufiles", $imageFile);		
		$newImage = array ("filename" => $filename, "title" => $gallery['title']);
		$images[] = $newImage;
		$gallery['images'] = serialize($images);
		try 
		{
			$gallery->save();
			return array("success" => true);
		}		
		catch (Exception $e)
		{
			return array("success" => false, "errorMessage" => $e->getMessage());
		} 
	}
	
	/*
	 * Update a title name (description) for an image 
	 */
	public function updateGalleryImageTitle($gallery, $imageFile, $newTitle)
	{
		if (!empty($gallery['images']))
			$images = unserialize ($gallery['images']);
		else
			return array("success" => false, "errorMessage" => "gallery empty");
		foreach ($images as $k=>$image)
		{
			if ($image['filename'] == $imageFile)
			{
				$images[$k]['title'] = $newTitle;
				break;
			}
		}		
		$gallery['images'] = serialize($images);
		try 
		{
			$gallery->save();
			return array("success" => true);
		}		
		catch (Exception $e)
		{
			return array("success" => false, "errorMessage" => $e->getMessage());
		} 
	
	}
	

	/*
	 * Remove an image from gallery 
	 */
	public function deleteGalleryImage($gallery, $imageFile)
	{
		if (!empty($gallery['images']))
			$images = unserialize ($gallery['images']);
		else
			return array("success" => false, "errorMessage" => "gallery empty");

		foreach ($images as $k=>$image)
		{
			if ($image['filename'] == $imageFile)
			{
				//TODO: remove all the images in the file system here as well
				unset($images[$k]);
				break;
			}
		}		
		$gallery['images'] = serialize($images);
		try 
		{
			$gallery->save();
			return array("success" => true);
		}		
		catch (Exception $e)
		{
			return array("success" => false, "errorMessage" => $e->getMessage());
		} 
	}

	
	public function postImage($createWM)
	{
		$watermarkDir = APP_PATH."/public/watermark/";
		$auth = Zend_Auth::getInstance();
		$userId = $auth->getIdentity()->id;
		$configSize = array("xsmall", "small", "medium", "large", "xlarge");
		$configSizeInPixel = array("75", "100", "260", "350", "600", "1300");
		$ustoreDir = APP_PATH."/public/ustore/$userId/images/original/gallery/";
		if (!file_exists($ustoreDir))
			mkdir($ustoreDir, 0777  , true);
			
		$adapter= new Zend_File_Transfer_Adapter_Http();
		$IsImageValidateor = new Clu_Validate_File_IsImage ();
		$isImage = $IsImageValidateor->isValid($_FILES['file_upload']['tmp_name'], $_FILES['file_upload']);
		$extension_path = pathinfo($_FILES['file_upload']['name']);
		$name = $extension_path['filename'];
		if($isImage)
		{
			$mimeType = $IsImageValidateor->getFileMimeType();
			$extension = explode('/',$mimeType);

			$fileName = $name.'.'.$extension['1'];
			$newfileName = SanitizeFileName($fileName);
			$originalFilename = $ustoreDir.$newfileName;
			$i = 1;
			while(file_exists($originalFilename))
			{
				$fileName = $name.'_'.$i.'.'.$extension['1'];
				$newfileName = SanitizeFileName($fileName);
				$originalFilename = $ustoreDir.$newfileName;
				$i++;
			}
			
			try 
			{
				$adapter->addFilter('Rename', array('target' => $originalFilename, 'overwrite' => true));
				$adapter->receive();
			} 
			catch (Zend_File_Transfer_Exception $e) 
			{
			     $e->getMessage();
			}
			chmod($originalFilename, 0666);
		}
		
		generate_image_copies($ustoreDir, $newfileName, $watermarkDir, $configSize, $configSizeInPixel, $userId, $createWM);
		
	}
}

?>
